<template>
  <div class="borrower">
    <el-form :model="ruleForm"
             :rules="rules"
             ref="ruleForm"
             label-width="100px"
             class="yuyi-ruleForm">

      <el-form-item label="档案类型" prop="fileType">
        <el-select v-model="ruleForm.fileType" style="width: 100%" placeholder="请选择证件类型">
          <el-option v-for="(item, index) in treeData"
                     :key="index"
                     :label="item.name"
                     :value="item.name"></el-option>
          <!--<el-option label="会计档案" value="会计档案"></el-option>
          <el-option label="科技档案" value="科技档案"></el-option>
          <el-option label="实物档案" value="实物档案"></el-option>
          <el-option label="音像档案" value="音像档案"></el-option>
          <el-option label="资料" value="资料"></el-option>-->
          <el-option label="其他" value="其他"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="档号" prop="fileCode">
        <el-input v-model="ruleForm.fileCode"></el-input>
      </el-form-item>
      <el-form-item label="名称" prop="fileName">
        <el-input v-model="ruleForm.fileName"></el-input>
      </el-form-item>
      <el-form-item label="备注">
        <el-input v-model="ruleForm.itemsRemarks"
                  type="textarea"
                  show-word-limit
                  :autosize="autosize"
                  maxlength="200"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button @click="submitForm(subType)" type="primary" size="small">{{subType === 'add' ? '登记' : '修改'}}</el-button>
        <el-button @click="submitForm('add')" size="small" v-if=" subType === 'edit' ">新增</el-button>
      </el-form-item>
    </el-form>

    <div class="list-box" v-if="tableData.length>0">
      <el-table
        :data="tableData"
        border
        class="table">
        <el-table-column
          align="center"
          type="index"
          label="序号"
          width="60">
        </el-table-column>
        <el-table-column
          show-overflow-tooltip
          prop="fileType"
          label="借阅类型">
        </el-table-column>
        <el-table-column
          show-overflow-tooltip
          prop="name"
          label="证件类型"
          width="100">
        </el-table-column>
        <el-table-column
          show-overflow-tooltip
          prop="fileCode"
          label="档号">
        </el-table-column>
        <el-table-column
          show-overflow-tooltip
          prop="fileName"
          label="名称">
        </el-table-column>
        <el-table-column
          show-overflow-tooltip
          prop="itemsRemarks"
          label="备注">
        </el-table-column>
        <el-table-column
          fixed="right"
          align="center"
          label="操作"
          width="150">
          <template slot-scope="scope">
            <el-button type="text" @click="handleAddEdit(scope.row, scope.$index)">编辑</el-button>
            <el-button @click="handleDel(scope.row, scope.$index)" type="text" style="color: #F56C6C">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <div class="fun-btn text-right" v-if="tableData.length>0">
      <el-button size="small" type="primary" @click="handleNext">{{type === 'add' ? '下一步' : '保存'}}</el-button>
<!--      <el-button size="small">重置</el-button>-->
    </div>

  </div>
</template>

<script>
import { getSelfCategoryTree } from '../../../../../api/category/category'

export default {
  name: 'borrowingGoods', // 借阅物品登记
  props: {
    type: {
      type: String,
      default: 'add'
    },
    formObj: {
      type: Object
    },
    idContinue: {
      type: String,
      default: ''
    }
  },
  data () {
    return {
      ruleForm: {
        fileType: '',
        fileCode: '',
        itemsRemarks: ''
      },
      autosize: {
        minRows: 4,
        maxRows: 8
      },
      rules: {
        fileType: [
          { required: true, message: '请选择档案类型', trigger: 'change' }
        ],
        fileCode: [
          { required: true, message: '请输入档号', trigger: 'blur' }
        ],
        fileName: [
          { required: true, message: '请输入名称', trigger: 'blur' }
        ]
      },
      subType: 'add',
      rowIndex: '',
      tableData: [],
      treeData: []
    }
  },
  mounted () {
    this.getCategory()
  },
  computed: {
    obj () {
      const items = this.formObj
      return items
    }
  },
  watch: {
    formObj: {
      handler: function () {
        this.tableData = this.obj.jywppab
      },
      deep: true
    }
  },
  methods: {
    getCategory () {
      getSelfCategoryTree('daModel', this.$route.query.isNotAjJnNode).then(res => {
        this.treeData = res.data.data
      })
    },
    submitForm (type) {
      // 登记/修改
      // 登记/修改
      if (this.type === 'edit' && type === 'add') {
        // 条目修改时 新增
        const dataObj = JSON.parse(JSON.stringify(this.ruleForm))
        dataObj.idContinue = this.idContinue
        const addObj = {
          jywppab: []
        }
        addObj.jywppab.push(dataObj)
        this.$emit('handleAdd', addObj)
        return
      }
      if (this.type === 'edit' && type === 'edit') {
        // 条目修改时 修改
        this.$set(this.tableData, this.rowIndex, JSON.parse(JSON.stringify(this.ruleForm)))
        this.obj.jywppab = this.tableData
        this.$emit('handleEdit', this.obj)
        return
      }

      if (type === 'add') {
        this.subType = 'add'
      }
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          if (this.subType === 'add') {
            this.tableData.push(JSON.parse(JSON.stringify(this.ruleForm)))
          } else if (this.subType === 'edit') {
            this.$set(this.tableData, this.rowIndex, JSON.parse(JSON.stringify(this.ruleForm)))
            this.$message({
              type: 'success',
              message: '修改成功!'
            })
          }
        } else {
          return false
        }
      })
    },
    resetForm (formName) {
      this.$refs[formName].resetFields()
    },
    handleAddEdit (row, index) {
      // 编辑
      this.subType = 'edit'
      this.rowIndex = index
      this.ruleForm = JSON.parse(JSON.stringify(row))
    },
    handleDel (row, index) {
      // 删除
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        if (this.type === 'add') {
          this.tableData.splice(index, 1)
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        }
        if (this.type === 'edit') {
          this.$emit('handleDel', row)
        }
      }).catch(() => { })
    },
    handleNext () {
      this.obj.jywppab = this.tableData
      this.$emit('handleNext', this.obj)
    }
  }
}
</script>

<style lang="scss" scoped>
  .borrower{
    .yuyi-ruleForm{
      position: relative;
      padding-top: 40px;
      .yuyi-upload{
        position: absolute;
        right: 0px;
        z-index: 2;
        border: 1px dashed #ccc;
        .el-upload {
          border: 1px dashed #d9d9d9;
          border-radius: 6px;
          cursor: pointer;
          position: relative;
          overflow: hidden;
          &:hover {
            border-color: #409EFF;
          }
        }
        .avatar-uploader-icon {
          font-size: 28px;
          color: #8c939d;
          width: 160px;
          height: 160px;
          line-height: 160px;
          text-align: center;
        }
        .avatar {
          width: 178px;
          height: 178px;
          display: block;
        }
      }
    }

    .list-box{
      margin-top: 15px;
      margin-bottom: 15px;
    }
  }

</style>
